/*
 * TabNacoesGui.java
 *
 * Created on April 23, 2008, 11:37 AM
 */
package gui.tabs;

import control.FinancasControler;
import control.MapaControler;
import gui.TabBase;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableModel;
import model.Nacao;
import model.PersonagemOrdem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import persistence.BundleManager;
import persistence.SettingsManager;

/**
 *
 * @author gurgel
 */
public final class TabFinancasGuiOld extends TabBase implements Serializable {

    private static final Log log = LogFactory.getLog(TabFinancasGuiOld.class);
    private static final BundleManager labels = SettingsManager.getInstance().getBundleManager();
    private FinancasControler financasControl;

    public TabFinancasGuiOld(String titulo, String dica, MapaControler mapaControl) {
        initComponents();
        //Basico
        setIcone("/images/financas.gif");
        setTitle(titulo);
        setDica(dica);
        this.setMapaControler(mapaControl);
        //configura grid
        jtMainLista.setAutoCreateColumnsFromModel(true);
        jtMainLista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtMainLista.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jtMainLista.setAutoCreateRowSorter(true);
        jtProdutos.setAutoCreateColumnsFromModel(true);
        jtProdutos.setAutoCreateRowSorter(true);
        jtProdutos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtProdutos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jtExtrato.setAutoCreateColumnsFromModel(true);
        jtExtrato.setAutoCreateRowSorter(true);
        jtExtrato.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtExtrato.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jtProj.setAutoCreateColumnsFromModel(true);
        jtProj.setAutoCreateRowSorter(true);
        jtProj.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtProj.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        comboFiltro.setName("comboFiltro");
        comboFiltro.setSelectedIndex(this.getFiltroDefault());
        //Cria o Controle da lista 
//        financasControl = new FinancasControler(this);

        //adiciona listeners
        comboFiltro.addActionListener(financasControl);
        jtMainLista.getSelectionModel().addListSelectionListener(financasControl);

        TableModel model = financasControl.getMainTableModel(this.getFiltro());
        this.setMainModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        comboFiltro = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        qtNacoes = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtMainLista = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtProdutos = new javax.swing.JTable();
        jpExtrato = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtExtrato = new javax.swing.JTable();
        jpProjecao = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jtProj = new javax.swing.JTable();

        jLabel2.setText(labels.getString("TOTAL:")); // NOI18N

        comboFiltro.setModel(getDefaultComboBoxModelTodosProprio());

        jLabel1.setText(labels.getString("LISTAR:")); // NOI18N

        qtNacoes.setText("66666"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(qtNacoes)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(qtNacoes))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtMainLista.setAutoCreateRowSorter(true);
        jtMainLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nação", "Tamanho", "Nação", "Local", "Title 5"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtMainLista.setName(""); // NOI18N
        jScrollPane3.setViewportView(jtMainLista);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(labels.getString("MERCADO"))); // NOI18N

        jtProdutos.setAutoCreateRowSorter(true);
        jtProdutos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Produto", "Unidades", "Compra", "Venda", "Estoques", "Produção"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtProdutos.setName(""); // NOI18N
        jScrollPane2.setViewportView(jtProdutos);

        jpExtrato.setBorder(javax.swing.BorderFactory.createTitledBorder(labels.getString("EXTRATO.TURNO"))); // NOI18N

        jtExtrato.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nome", "Valor"
            }
        ));
        jScrollPane4.setViewportView(jtExtrato);

        javax.swing.GroupLayout jpExtratoLayout = new javax.swing.GroupLayout(jpExtrato);
        jpExtrato.setLayout(jpExtratoLayout);
        jpExtratoLayout.setHorizontalGroup(
            jpExtratoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
        );
        jpExtratoLayout.setVerticalGroup(
            jpExtratoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
        );

        jpProjecao.setBorder(javax.swing.BorderFactory.createTitledBorder(labels.getString("EXTRATO.PROXIMO.TURNO"))); // NOI18N

        jtProj.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nome", "Valor"
            }
        ));
        jScrollPane5.setViewportView(jtProj);

        javax.swing.GroupLayout jpProjecaoLayout = new javax.swing.GroupLayout(jpProjecao);
        jpProjecao.setLayout(jpProjecaoLayout);
        jpProjecaoLayout.setHorizontalGroup(
            jpProjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
        );
        jpProjecaoLayout.setVerticalGroup(
            jpProjecaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jpExtrato, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpProjecao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpProjecao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpExtrato, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboFiltro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JPanel jpExtrato;
    private javax.swing.JPanel jpProjecao;
    private javax.swing.JTable jtExtrato;
    private javax.swing.JTable jtMainLista;
    private javax.swing.JTable jtProdutos;
    private javax.swing.JTable jtProj;
    private javax.swing.JLabel qtNacoes;
    // End of variables declaration//GEN-END:variables

    public JTable getMainLista() {
        return jtMainLista;
    }

    public void setMainModel(TableModel model) {
        this.setProdutosModel(null);
        this.setExtratoModel(null);
        this.setProjecaoModel(null);
        this.jtMainLista.setModel(model);
        this.calcColumnWidths(jtMainLista);
        this.updateGui();
        this.doTagHide();
        this.jtMainLista.getSelectionModel().setSelectionInterval(0, 0);
    }

    public int getFiltro() {
        return comboFiltro.getSelectedIndex();
    }

    private void updateGui() {
        this.qtNacoes.setText(getMainLista().getRowCount() + "");
    }

    private void setProdutosModel(TableModel model) {
        if (model == null) {
            Object[][] data = new Object[][]{
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}};
            String[] header = new String[]{labels.getString("PRODUTO"), labels.getString("UNIDADES"), labels.getString("COMPRA"), labels.getString("VENDA"), labels.getString("ESTOQUES"), labels.getString("PRODUCAO")};
            javax.swing.table.DefaultTableModel dtm = new javax.swing.table.DefaultTableModel(data, header) {
                Class[] types = new Class[]{
                    java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
                };

                @Override
                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }
            };
            jtProdutos.setModel(dtm);
        } else {
            this.jtProdutos.setModel(model);
            calcColumnWidths(jtProdutos);
        }
    }

    public void setProjecaoModel(TableModel model) {
        if (model == null) {
            this.jtProj.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{
                        {null, null, null},
                        {null, null, null},
                        {null, null, null},
                        {null, null, null}},
                    new String[]{
                        labels.getString("DESCRICAO"), labels.getString("VALOR"), labels.getString("SALDO")
                    }));
        } else {
            this.jtProj.setModel(model);
            calcColumnWidths(jtProj);
        }
    }

    private void setExtratoModel(TableModel model) {
        if (model == null) {
            this.jtExtrato.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{
                        {null, null, null},
                        {null, null, null},
                        {null, null, null},
                        {null, null, null}},
                    new String[]{
                        labels.getString("DESCRICAO"), labels.getString("VALOR"), labels.getString("SALDO")
                    }));
        } else {
            this.jtExtrato.setModel(model);
            calcColumnWidths(jtExtrato);
        }
    }

    public void doMudaNacao(Nacao nacao) {
        try {
            getMapaControler().printTag(nacao.getCapital().getLocal());
        } catch (NullPointerException ex) {
            this.doTagHide();
        }
        this.setProdutosModel(financasControl.getMercadoTableModel(nacao));
        this.setExtratoModel(financasControl.getExtratoTableModel(nacao));
        this.setProjecaoModel(financasControl.getProjecaoTableModel(nacao, new ArrayList<PersonagemOrdem>()));
    }
}
